<?php
class item extends dao
{
	protected $table = "Items";
	
	protected function create()
	{
		$connection = db::factory('mysql');
		
		$keys = array();
		$values = array();
		
		foreach($this->values as $key=>$value){
			$keys[] = "`$key`";
			switch($key){
				case 'mapdata':
					$values[] = "GeomFromText('" . $connection->clean($value) . "')";
					break;
				default:
					$values[] = "'{$connection->clean($value)}'";
			}
		}		

		$sql = "INSERT INTO `{$this->table}` (";
		$sql .= implode(",", $keys);
		$sql .= ")VALUES(";
		$sql .= implode(",", $values);
		$sql .= ")";
		
		$this->id = $connection->insertGetID($sql);
	}
	
	protected function update()
	{
		$connection = db::factory('mysql');
		
		$sql = "UPDATE {$this->table} set ";
		
		$updates = array();
		foreach($this->values as $key=>$value){
			switch($key){
				case 'mapdata':					
					$updates[] = "`{$key}`=GeomFromText('" . $connection->clean($value) . "')";
					break;
				default:
					$values[] = "'{$connection->clean($value)}'";
					$updates[] = "`{$key}`='" . $connection->clean($value) . "'";
			}
		}
		
		$sql .= implode(', ', $updates);
		$sql .= " WHERE id={$this->id}";
		
		$connection->execute($sql);
	}
}